﻿using FB.Mvc.Controllers;
using FCDB.Entity;
using FCDB.Entity.EntityEx;
using FCDB.Repository;
using FCDB.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace FCDB.Web.Controllers
{
      [FB.Membership.Annotations.AuthorizeOnly]
    public class FormDraftController : BaseController
    {
        //
        // GET: /FormDraft/
        private FormRepository formRepository = new FormRepository();
        public ActionResult Index()
        {
            if (Request.IsAjaxRequest())
            {
                int total = 0, take = 10, skip = 0;
                int.TryParse(Request["take"], out take);
                int.TryParse(Request["skip"], out skip);
                string dtStart = Request["dtStart"];
                string dtEnd = Request["dtEnd"];
                string kewyword = Request["keyword"];
                IList<FormDraft> list = formRepository.GetFormDrafts(skip, take, this.UserID, out total, dtStart
                    , dtEnd, kewyword);
                PagerModel model = new PagerModel()
                {
                    Data = list,
                    TotalCount = total
                };
                JsonResult json = new JsonResult()
                {
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                    Data = model
                };
                return json;
            }

            return View();
        }

        public ActionResult ViewDraftDetail()
        {
            if (string.IsNullOrEmpty(Request["FormID"])) return Content("没有存在该表单");
            string ID = Request["FormID"];
            string FormAction = Request["FormAction"];
            string ProccessGuid = Request["ProccessGuid"];
            string url = string.Format("~/Form/{0}?FormID={1}&ProcessGuid={2}", FormAction, ID, ProccessGuid);
            return Redirect(url);
        }

        public ActionResult DeleteMyDraft()
        {
            if (string.IsNullOrEmpty(Request["id"])) return Content("没有存在该表单");
            string idStr = Request["id"];
            string FormAction = Request["FormAction"];
            FormRepository repository = new FormRepository();
            long ID = long.Parse(idStr);
              int flag = 0;
            if(FormAction.Equals("FormNoContractPay"))
            {
                FormNoContractPay draft = repository.Get<FormNoContractPay>(ID);
                flag = repository.Delete(draft);
            }
            if (FormAction.Equals("FormCDB"))
            {
                FormCDB draft = repository.Get<FormCDB>(ID);
                flag = repository.Delete(draft);
            }
            if (FormAction.Equals("FormCost"))
            {
                FormCost draft = repository.Get<FormCost>(ID);
                flag = repository.Delete(draft);
            }
           
            RtnMsg result = new RtnMsg()
            {
                Status = 1
            };
            if (flag >0)
                result.Status = 1;

            else
                result.Status = 2;
            return Json(result);
      }
    }
}
